package com.kaka.bus.engine.user.register;

import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.di.DataManager;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.mvp.RxPresenter;

import javax.inject.Inject;

/**
 * Created by zhangqi on 2016/9/28.
 * e-mail : ${email}
 * desc :
 */

public class RegistPresenter extends RxPresenter<RegistContract.View> implements RegistContract.Presenter {

    @Inject
    public RegistPresenter(DataManager dataManager) {
        super(dataManager);
    }

    @Override
    public void isExist(String phone) {
        mView.showDialog("验证手机号码...");
        mDataManager
                .getApi()
                .phoneIsExist(phone)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<String>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<String> bean) {
                        mView.phoneState(bean);
                    }

                    @Override
                    protected void onFinish() {
                        mView.dissDialog();
                    }
                });
    }


    @Override
    public boolean sendyzm(String phone, String tokenKey) {
        /**
         * 发送验证码  发送成功时 应该记住当前发送的号码 防止用户更改号码
         */
        mView.showDialog("发送验证码...");

        addSubscribe(mDataManager
                .getApi()
                .getCode(phone,tokenKey,"register", Constant.PLATFORM)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<String>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<String> bean) {
                        mView.sendSmsSuccess(bean);
                    }

                    @Override
                    protected void onFinish() {
                        mView.dissDialog();
                    }
                }));

        return true;
    }

    @Override
    public void checkInvite(String phone,String invite) {
        mView.showDialog("校验邀请码...");
        mDataManager
                .getApi()
                .checkInviteCode(phone,invite)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<String>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<String> bean) {
                        mView.checkInviteResult(bean);
                    }

                    @Override
                    protected void onFinish() {
                        mView.dissDialog();
                    }
                });
    }


}
